<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title data-i18n="resources.title_layerSwitch"></title>
    <script type="text/javascript" src="../js/include-web.js"></script>
    <script type="text/javascript" src="../../dist/ol/include-ol.js"></script>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:95%; position: absolute;top: 0;">
<div id="map" style="width: 100%;height:95%"></div>
<input id="swipe" type="range" style="width: 100%;">
<script type="text/javascript">
    var host = window.isLocal ? window.server : "https://iserver.supermap.io";
    var worldurl = host + '/iserver/services/map-world/rest/maps/World',
        worldNighturl = host + '/iserver/services/map-world/rest/maps/世界地图_Night';

    var world = new ol.layer.Tile({
        source: new ol.source.TileSuperMapRest({
            url: worldurl
        }),
        projection: 'EPSG:4326'
    });

    var worldNight = new ol.layer.Tile({
        source: new ol.source.TileSuperMapRest({
            url: worldNighturl
        }),
        projection: 'EPSG:4326'
    });

    var map = new ol.Map({
        layers: [world, worldNight],
        target: 'map',
        // ol v7版本用法为ol.control.defaults.defaults； v6版本以下用法为ol.control.defaults
        controls: ol.control.defaults.defaults({attributionOptions: {collapsed: false}}),
        view: new ol.View({
            center: [0, 0],
            zoom: 3,
            projection: 'EPSG:4326',
            multiWorld: true
        })
    });

    var swipe = document.getElementById('swipe');
    // ol6 废除了 precompose，由 prerender 事件替换
    worldNight.on('prerender', function (event) {
        var ctx = event.context;
        var width = ctx.canvas.width * (swipe.value / 100);

        ctx.save();
        ctx.beginPath();
        ctx.rect(width, 0, ctx.canvas.width - width, ctx.canvas.height);
        ctx.clip();
    });
    // ol6 废除了 postcompose，由 postrender 事件替换
    worldNight.on('postrender', function (event) {
        var ctx = event.context;
        ctx.restore();
    });

    swipe.addEventListener('input', function () {
        map.render();
    }, false);
</script>
</body>
</html>